using System;
using System.Collections.Generic;
using System.Text;
using DataAccess;
using NHibernate;
using System.Collections;

namespace Task
	{
	public sealed class SearchTask
		{
		public List<DataAccess.forum> SearchForums(string searchedText)
			{
			
			ISession session = NHibernateSessionManager.Instance.GetSession();
			ICriteria criteria = session.CreateCriteria(typeof(forum))
								.CreateAlias("user", "usr")
								.Add(NHibernate.Expression.Expression.Or(NHibernate.Expression.Expression.Or(NHibernate.Expression.Expression.Like("name", "%" + searchedText + "%"),
				NHibernate.Expression.Expression.Like("description", "%" + searchedText + "%")), NHibernate.Expression.Expression.Like("usr.login", "%" + searchedText + "%")));
			List<forum> returnList = new List<forum>();
			returnList.AddRange(criteria.List<forum>());
			return returnList;
			}

		public List<DataAccess.art> SearchArt(string searchedText)
			{
			ISession session = NHibernateSessionManager.Instance.GetSession();
			ICriteria criteria = session.CreateCriteria(typeof(art))
								.CreateAlias("user", "usr")
								.Add(NHibernate.Expression.Expression.Or(NHibernate.Expression.Expression.Or(NHibernate.Expression.Expression.Or(NHibernate.Expression.Expression.Like("name", "%" + searchedText + "%"),
				NHibernate.Expression.Expression.Like("description", "%" + searchedText + "%")), NHibernate.Expression.Expression.Like("usr.login", "%" + searchedText + "%")), NHibernate.Expression.Expression.Like("text", "%" + searchedText + "%")));
			List<art> returnList = new List<art>();
			returnList.AddRange(criteria.List<art>());
			return returnList;
			}

		public List<DataAccess.comment> SearchComments(string searchedText)
			{

			ISession session = NHibernateSessionManager.Instance.GetSession();
			ICriteria criteria = session.CreateCriteria(typeof(comment))
								.CreateAlias("user", "usr")
								.Add(NHibernate.Expression.Expression.Or(NHibernate.Expression.Expression.Like("contents", "%" + searchedText + "%"), NHibernate.Expression.Expression.Like("usr.login", "%" + searchedText + "%")));
			List<comment> returnList = new List<comment>();
			returnList.AddRange(criteria.List<comment>());
			return returnList;
			}

		public List<DataAccess.gallery> SearchGaleries(string searchedText)
			{

			ISession session = NHibernateSessionManager.Instance.GetSession();
			ICriteria criteria = session.CreateCriteria(typeof(gallery))
								.CreateAlias("user", "usr")
								.Add(NHibernate.Expression.Expression.Or(NHibernate.Expression.Expression.Or(NHibernate.Expression.Expression.Like("name", "%" + searchedText + "%"),
				NHibernate.Expression.Expression.Like("description", "%" + searchedText + "%")), NHibernate.Expression.Expression.Like("usr.login", "%" + searchedText + "%")));
			List<gallery> returnList = new List<gallery>();
			returnList.AddRange(criteria.List<gallery>());
			return returnList;
			}

		public List<DataAccess.user> SearchUsers(string searchedText)
			{

			ISession session = NHibernateSessionManager.Instance.GetSession();
			ICriteria criteria = session.CreateCriteria(typeof(user)).Add(NHibernate.Expression.Expression.Or(NHibernate.Expression.Expression.Or(NHibernate.Expression.Expression.Like("login", "%" + searchedText + "%"), NHibernate.Expression.Expression.Or(NHibernate.Expression.Expression.Or(NHibernate.Expression.Expression.Like("email", "%" + searchedText + "%"), NHibernate.Expression.Expression.Like("gg", "%" + searchedText + "%")), NHibernate.Expression.Expression.Like("skype", "%" + searchedText + "%"))), NHibernate.Expression.Expression.Or(NHibernate.Expression.Expression.Like("name", "%" + searchedText + "%"), NHibernate.Expression.Expression.Like("surname", "%" + searchedText + "%"))));					
			List<user> returnList = new List<user>();
			returnList.AddRange(criteria.List<user>());
			return returnList;
			}

		public List<DataAccess.art> SearchArtsByTag(string searchedText)
			{
			//art a = new art();
			//a.
			NHibernateDao<art,int> sampleArtFactory = new NHibernateDao<art,int>();
			List<art> returnList = new List<art>();
			ISession session = NHibernateSessionManager.Instance.GetSession();
			IList result = session.CreateQuery("select a.id from art as a join a.art_tags as att join att.tag as t where t.name like '%" + searchedText.ToString() + "%'").List();
			foreach (int id in result)
				{
				returnList.Add(sampleArtFactory.GetById(id,false));
				}
			
			
			return returnList;
			}

		}
	}
